package managedbean.Sales;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.ejb.EJB;
import session.stateless.EmployeeSessionBeanLocal;
import javax.faces.bean.ManagedProperty;
import managedbean.UserSessionManagerBean;
import entity.Address;
import entity.Customer;
import java.util.ArrayList;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

@ManagedBean(name = "addressManagerBean")
@RequestScoped
public class AddressManagerBean {

    @ManagedProperty(value = "#{userSessionManagerBean}")
    private UserSessionManagerBean userSessionManagerBean;
    @EJB
    private EmployeeSessionBeanLocal employeeSessionBeanLocal;
    private ArrayList<Address> addresses;
    private Address address;
    private Customer customer;
    private String company;
    private String street;
    private Integer postal;
    private String country;
    private String city;
    private Address selectedAddress;
    private String loadAddAddress;
    private String type;

    public String getLoadAddAddress() {


        if (getUserSessionManagerBean().getCustomer() != null) {
            setCompany(getUserSessionManagerBean().getCustomer().getCompany());
        }

        setAddresses(new ArrayList<Address>());

        if (getUserSessionManagerBean().getCustomer().getAddresses() != null) {
            if (((String) getUserSessionManagerBean().getSelectedItem()).compareTo("Shipping") == 0) {

                for (Address a : getUserSessionManagerBean().getCustomer().getAddresses()) {
                    if (a.getType().compareTo("Shipping") == 0) {
                        getAddresses().add(a);
                    }
                }

            } else {
                for (Address a : getUserSessionManagerBean().getCustomer().getAddresses()) {
                    if (a.getType().compareTo("Billing") == 0) {
                        getAddresses().add(a);
                    }
                }
            }
        }
        return null;
    }

    // <editor-fold defaultstate="collapsed" desc="Customer attributes. Click on the + sign on the left to edit the code.">
    /**
     * @return the userSessionManagerBean
     */
    public UserSessionManagerBean getUserSessionManagerBean() {
        return userSessionManagerBean;
    }

    /**
     * @param userSessionManagerBean the userSessionManagerBean to set
     */
    public void setUserSessionManagerBean(UserSessionManagerBean userSessionManagerBean) {
        this.userSessionManagerBean = userSessionManagerBean;
    }

    /**
     * @return the employeeSessionLocal
     */
    public EmployeeSessionBeanLocal getEmployeeSessionBeanLocal() {
        return employeeSessionBeanLocal;
    }

    /**
     * @param employeeSessionLocal the employeeSessionLocal to set
     */
    public void setEmployeeSessionBeanLocal(EmployeeSessionBeanLocal employeeSessionBeanLocal) {
        this.employeeSessionBeanLocal = employeeSessionBeanLocal;
    }

    /**
     * @return the addresses
     */
    public ArrayList<Address> getAddresses() {
        return addresses;
    }

    /**
     * @param addresses the addresses to set
     */
    public void setAddresses(ArrayList<Address> addresses) {
        this.addresses = addresses;
    }

    /**
     * @return the address
     */
    public Address getAddress() {
        return address;
    }

    /**
     * @param address the address to set
     */
    public void setAddress(Address address) {
        this.address = address;
    }

    /**
     * @return the customer
     */
    public Customer getCustomer() {
        return customer;
    }

    /**
     * @param customer the customer to set
     */
    public void setCustomer(Customer customer) {
        this.customer = customer;
    }

    /**
     * @return the company
     */
    public String getCompany() {
        return company;
    }

    /**
     * @param company the company to set
     */
    public void setCompany(String company) {
        this.company = company;
    }

    /**
     * @return the street
     */
    public String getStreet() {
        return street;
    }

    /**
     * @param street the street to set
     */
    public void setStreet(String street) {
        this.street = street;
    }

    /**
     * @return the postal
     */
    public Integer getPostal() {
        return postal;
    }

    /**
     * @param postal the postal to set
     */
    public void setPostal(Integer postal) {
        this.postal = postal;
    }

    /**
     * @return the country
     */
    public String getCountry() {
        return country;
    }

    /**
     * @param country the country to set
     */
    public void setCountry(String country) {
        this.country = country;
    }

    /**
     * @return the city
     */
    public String getCity() {
        return city;
    }

    /**
     * @param city the city to set
     */
    public void setCity(String city) {
        this.city = city;
    }

    /**
     * @return the selectedAddress
     */
    public Address getSelectedAddress() {
        return selectedAddress;
    }

    /**
     * @param selectedAddress the selectedAddress to set
     */
    public void setSelectedAddress(Address selectedAddress) {
        this.selectedAddress = selectedAddress;
    }

    /**
     * @return the type
     */
    public String getType() {
        return type;
    }

    /**
     * @param type the type to set
     */
    public void setType(String type) {
        this.type = type;
    }

    public void setLoadAddAddress(String loadAddAddress) {
        this.loadAddAddress = loadAddAddress;
    }

    //</editor-fold>
    public void doneAddress(ActionEvent event) {
        try {
            if (userSessionManagerBean.getCustomer().getCustId() == null) {
                FacesContext.getCurrentInstance().getExternalContext().redirect("http://" + userSessionManagerBean.getServerName() + ":"
                        + userSessionManagerBean.getServerPort() + "/MerlionERP-war/Web_Sales/addCustomer.xhtml");
            } else {
                FacesContext.getCurrentInstance().getExternalContext().redirect("http://" + userSessionManagerBean.getServerName() + ":"
                        + userSessionManagerBean.getServerPort() + "/MerlionERP-war/Web_Sales/updateCustomer.xhtml");
            }
        } catch (Exception ex) {
        }
    }

    public void removeAdddress(ActionEvent event) {
        int count = 0;
        for (Address a : userSessionManagerBean.getCustomer().getAddresses()) {
            if ((a.getType()).compareTo(getSelectedAddress().getType()) == 0) {
                count++;
            }
        }

        if (count > 1) {
            userSessionManagerBean.getCustomer().getAddresses().remove(getSelectedAddress());

            try {

                FacesContext.getCurrentInstance().getExternalContext().redirect("http://"
                        + userSessionManagerBean.getServerName() + ":"
                        + userSessionManagerBean.getServerPort()
                        + "/MerlionERP-war/Web_Sales/addAddress.xhtml"); //redirect to messages.xhtml after deleting message
            } catch (Exception ex) {
            }
        }
        else
        {
            FacesContext.getCurrentInstance().addMessage(null,
                    new FacesMessage(FacesMessage.SEVERITY_ERROR, "Address cannot be 0.", ""));
        }



    }

    public void saveAddress(ActionEvent event) {

        Address address;
        if (((String) userSessionManagerBean.getSelectedItem()).compareTo("Shipping") == 0) {
            address = new Address(getStreet(), getCountry(), getCity(), getPostal(), "Shipping");
        } else {
            address = new Address(getStreet(), getCountry(), getCity(), getPostal(), "Billing");

        }

        if (userSessionManagerBean.getCustomer().getAddresses() == null) {
            userSessionManagerBean.getCustomer().setAddresses(new ArrayList<Address>());
        }
        userSessionManagerBean.getCustomer().getAddresses().add(address);

        try {

            FacesContext.getCurrentInstance().getExternalContext().redirect("http://" + userSessionManagerBean.getServerName() + ":"
                    + userSessionManagerBean.getServerPort() + "/MerlionERP-war/Web_Sales/addAddress.xhtml");


        } catch (Exception ex) {
        }

    }
}